package demo.jdk8;

import java.text.SimpleDateFormat;
import java.time.Clock;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.FormatStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.function.Consumer;

import org.junit.Test;

public class DateAPITest {
	public static Consumer<Object>  print = System.out::println;
	
	@Test
	public void Clock(){
		Instant timestamp = Instant.now(); 
		System.out.println("What is value of this instant " + timestamp); 
		
		Clock clock = Clock.systemDefaultZone();
		long millis = clock.millis();
		print.accept(millis);
		Instant instant = clock.instant();
		Date legacyDate = Date.from(instant);   // legacy java.util.Date
		print.accept(legacyDate);
		
		// Returns the current time based on your system clock and set to UTC. 
		clock = Clock.systemUTC(); 
		System.out.println("Clock : " + clock); 
		System.out.println("Date : " + Date.from(clock.instant())); 

		ZoneId america = ZoneId.of("America/New_York"); 
		LocalDateTime localtDateAndTime = LocalDateTime.now(); 
		ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime, america ); 
		System.out.println("Current date and time in a particular timezone : " + dateAndTimeInNewYork); 


	}
	
    
	@Test
	 public void testLocalDate() {
		LocalDate today = LocalDate.now();
		LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
		@SuppressWarnings("unused")
		LocalDate yesterday = tomorrow.minusDays(2);
	
		LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
		DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
		print.accept(dayOfWeek);    // FRIDAY
		
		DateTimeFormatter germanFormatter =
				DateTimeFormatter
				.ofLocalizedDate(FormatStyle.MEDIUM)
				.withLocale(Locale.GERMAN);
		LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter);
		print.accept(xmas);   // 2014-12-24
		
		LocalDate java8Release = LocalDate.of(2014, Month.MARCH, 14); 
		Period periodToNextJavaRelease =  Period.between(today, java8Release); 
		System.out.println("Months left between today and Java8release : " + periodToNextJavaRelease.getMonths() ); 

	}
	
	@Test
	 public void testLocalDateTime() {
		LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59);
		DayOfWeek dayOfWeek = sylvester.getDayOfWeek();
		System.out.println(dayOfWeek);      // WEDNESDAY
		Month month = sylvester.getMonth();
		System.out.println(month);          // DECEMBER
		long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY);
		System.out.println(minuteOfDay); // 1439
		
		Instant instant = sylvester
				.atZone(ZoneId.systemDefault())
				.toInstant();
		Date legacyDate = Date.from(instant);
		System.out.println(legacyDate);     // Wed Dec 31 23:59:59 CET 2014
		
		DateTimeFormatter formatter = DateTimeFormatter
				.ofPattern("MMM dd, yyyy - HH:mm");
		LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter);
		String string = formatter.format(parsed);
		System.out.println(string);     // Nov 03, 2014 - 07:13
	}
	
	@Test
	 public void testDateTimeFormatter() {
		String dayAfterTommorrow = "20140116"; 
		LocalDate formatted = LocalDate.parse(dayAfterTommorrow,  DateTimeFormatter.BASIC_ISO_DATE); 
		System.out.printf("Date generated from String %s is %s %n", dayAfterTommorrow, formatted); 

		String goodFriday = "20140618"; 
		try {
			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); 	
			LocalDate holiday = LocalDate.parse(goodFriday, formatter); 
			System.out.printf("Successfully parsed String %s, date is %s%n", goodFriday, holiday); 
		} catch (DateTimeParseException ex) { 
			System.out.printf("%s is not parsable!%n", goodFriday); 
			ex.printStackTrace(); 
		}
	}
}
